﻿#include <QMessageBox>
#include "PageTestAnalyse.h"
#include "ui_PageTestAnalyse.h"
#include "GlobalData.h"
#include "../DatabaseMgmt/ExteriorTest.h"
#include "../DatabaseMgmt/GoodsStandard.h"
#include "../DatabaseMgmt/PhysicochemicalAnalyse.h"
#include "../DatabaseMgmt/ElementValue.h"

CPageTestAnalyse::CPageTestAnalyse(QWidget* parent /*= 0*/, Qt::WindowFlags f /*= 0*/)
	: QWidget(parent, f)
{
	init();
}

CPageTestAnalyse::~CPageTestAnalyse()
{

}

void CPageTestAnalyse::openTestReport()
{
	// 打开外观检测
	openExteriorTest();
	// 打开报告理化分析
	openPhysicochemicalAnalyse();
	// 打开元素检测数值
	openElementValue();
}

void CPageTestAnalyse::savePage()
{
	slotSave();
}

void CPageTestAnalyse::clear()
{
	m_ui->teInclusion->clear();
	m_ui->teUnbreakable->clear();
	m_ui->teElementTest->clear();
	m_ui->teRadialTest->clear();


	for (int i = 0; i < m_ui->twElement->rowCount(); ++i)
	{
		QTableWidgetItem* pValueItem = m_ui->twElement->item(i, 3);
		pValueItem->setText("");
		pValueItem->setData(Qt::UserRole, QVariant());

		QTableWidgetItem* pResultItem = m_ui->twElement->item(i, 4);
		pResultItem->setText("");
	}
}

void CPageTestAnalyse::init()
{
	m_ui = new Ui::PageTestAnalyse();
	m_ui->setupUi(this);

	initElementTable();

	connect(m_ui->btnSave, SIGNAL(clicked()), this, SLOT(slotSave()));
	connect(m_ui->twElement, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(slotItemChanged(QTableWidgetItem*)));
}

void CPageTestAnalyse::initElementTable()
{
	m_ui->twElement->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

	CGoodsStandard standard;
	QVector<GoodsStandard> elements;
	if (standard.queryAll(elements) != CONST_ERR_SUCCESS)
	{
		return;
	}
	m_ui->twElement->setRowCount(0);

	for (int i = 0; i < elements.size(); ++i)
	{
		int cloumn = 0;
		m_ui->twElement->insertRow(m_ui->twElement->rowCount());

		// 元素名
		QTableWidgetItem* pItem = new QTableWidgetItem(elements.at(i).name);
		pItem->setData(Qt::UserRole, elements.at(i).uid);
		pItem->setFlags(pItem->flags() & Qt::ItemIsEnabled);
		pItem->setTextAlignment(Qt::AlignCenter);
		m_ui->twElement->setItem(i, cloumn++, pItem);

		// 最小值
		pItem = new QTableWidgetItem(QString::number(elements.at(i).min_value, 'f', 4));
		pItem->setData(Qt::UserRole, elements.at(i).min_value);
		pItem->setFlags(pItem->flags() & Qt::ItemIsEnabled);
		pItem->setTextAlignment(Qt::AlignCenter);
		m_ui->twElement->setItem(i, cloumn++, pItem);

		// 最大值
		pItem = new QTableWidgetItem(QString::number(elements.at(i).max_value, 'f', 4));
		pItem->setData(Qt::UserRole, elements.at(i).max_value);
		pItem->setFlags(pItem->flags() & Qt::ItemIsEnabled);
		pItem->setTextAlignment(Qt::AlignCenter);
		m_ui->twElement->setItem(i, cloumn++, pItem);

		// 检测值
		pItem = new QTableWidgetItem();
		pItem->setTextAlignment(Qt::AlignCenter);
		m_ui->twElement->setItem(i, cloumn++, pItem);

		// 结果
		pItem = new QTableWidgetItem();
		pItem->setFlags(pItem->flags() & Qt::ItemIsEnabled);
		pItem->setTextAlignment(Qt::AlignCenter);
		m_ui->twElement->setItem(i, cloumn++, pItem);
	}
}

void CPageTestAnalyse::saveExteriorTest()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}
	ExteriorTest test;
	test.report_uid = nTestReportId;
	test.inclusion = m_ui->teInclusion->toPlainText().trimmed();
	test.unbreakable = m_ui->teUnbreakable->toPlainText().trimmed();
	CExteriorTest exterior;
	exterior.removeByReportUid(nTestReportId);

	int ret = exterior.insertRow(test);
	if (ret != CONST_ERR_SUCCESS)
	{
		qInfo() << QStringLiteral("保存外观检测数据错误：") << ret;
	}

}

void CPageTestAnalyse::savePhysicochemicalAnalyse()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	CPhysicochemicalAnalyse phyhcalys;

	phyhcalys.removeByReportUid(nTestReportId);

	PhysicochemicalAnalyse data;
	data.report_uid = nTestReportId;
	data.element_test = m_ui->teElementTest->toPlainText();
	data.radial_test = m_ui->teRadialTest->toPlainText();
	data.analyse_kind = m_ui->rbtnRule->isChecked() ? TT_Rule : TT_Radial;

	int ret = phyhcalys.insertRow(data);
	if (ret != CONST_ERR_SUCCESS)
	{
		qInfo() << QStringLiteral("插入理化分析数据失败，错误码：") << ret;
		return;
	}
}

void CPageTestAnalyse::saveElementValue()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}
	CElementValue element;

	element.removeByReportUid(nTestReportId);

	for (int i = 0; i < m_ui->twElement->rowCount(); ++i)
	{
		QTableWidgetItem* pItemElement = m_ui->twElement->item(i, 0);

		QTableWidgetItem* pItemValue = m_ui->twElement->item(i, 3);

		ElementValue data;
		data.report_uid = nTestReportId;
		data.element_uid = pItemElement->data(Qt::UserRole).toUInt();
		data.element = pItemValue->text().toDouble();
		

		int ret = element.insertRow(data);
		if (ret != CONST_ERR_SUCCESS)
		{
			qInfo() << QStringLiteral("插入元素数值") << pItemElement->text() << QStringLiteral("错误，错误码：") << ret;
		}
	}
}

void CPageTestAnalyse::openExteriorTest()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}
	ExteriorTest test_data;

	CExteriorTest exterior;
	int ret = exterior.queryByReportUid(nTestReportId, test_data);
	if (ret != CONST_ERR_SUCCESS)
	{
		qInfo() << QStringLiteral("打开外观检测数据错误：") << ret;
		return;
	}
	m_ui->teInclusion->setPlainText(test_data.inclusion);
	m_ui->teUnbreakable->setPlainText(test_data.unbreakable);
}

void CPageTestAnalyse::openPhysicochemicalAnalyse()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	CPhysicochemicalAnalyse phyhcalys;
	PhysicochemicalAnalyse data;
	
	int ret = phyhcalys.queryByReportUid(nTestReportId, data);
	if (ret != CONST_ERR_SUCCESS)
	{
		qInfo() << QStringLiteral("查询理化分析数据失败，错误码：") << ret;
		return;
	}
	if (!data.element_test.isEmpty())
	{
		m_ui->teElementTest->setPlainText(data.element_test);
	}
	if (!data.radial_test.isEmpty())
	{
		m_ui->teRadialTest->setPlainText(data.radial_test);
	}
	
	if (data.analyse_kind == TT_Rule)
	{
		m_ui->rbtnRule->setChecked(true);
	}
	else if (data.analyse_kind == TT_Radial)
	{
		m_ui->rbtnRadial->setChecked(true);
	}
}

void CPageTestAnalyse::openElementValue()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}
	CElementValue element;
	QVector<ElementValue> dataes;

	element.queryByReportUid(nTestReportId, dataes);

	for (int i = 0;i < dataes.size(); ++i)
	{
		for (int j = 0; j < m_ui->twElement->rowCount(); ++j)
		{
			// 元素名称
			QTableWidgetItem* pItemElement = m_ui->twElement->item(j, 0);
			// 元素值
			QTableWidgetItem* pItemValue = m_ui->twElement->item(j, 3);

			uint32_t element_uid = pItemElement->data(Qt::UserRole).toUInt();

			if (element_uid == dataes.at(i).element_uid)
			{
				pItemValue->setText(QString::number(dataes.at(i).element, 'f', 4));
				break;
			}
		}
	}
}

void CPageTestAnalyse::slotSave()
{
	// 保存外观检测
	saveExteriorTest();
	// 保存理化分析
	savePhysicochemicalAnalyse();
	// 保存元素数值
	saveElementValue();
}

void CPageTestAnalyse::slotItemChanged(QTableWidgetItem* pItem)
{
	if (!pItem)
	{
		return;
	}
	if (pItem->column() != 3)
	{
		qInfo() << QStringLiteral("选择列错误：") << pItem->row();
		return;
	}
	QString strText = pItem->text();

	if (strText.isEmpty())
	{
		return;
	}
	bool bSuccess = false;
	strText.toDouble(&bSuccess);

	if (!bSuccess)
	{
		qInfo() << QStringLiteral("数据的数据非数字：") << strText;
		QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("请输入数字！"));
		pItem->setText("");
	}
	else
	{
		if (pItem->text().isEmpty())
		{
			return;
		}
		double dValue = pItem->text().toDouble();
		pItem->setData(Qt::UserRole, dValue);
		int row = pItem->row();

		QTableWidgetItem* pMinItem = m_ui->twElement->item(row, 1);
		QTableWidgetItem* pMaxtem = m_ui->twElement->item(row, 2);

		QTableWidgetItem* pResultItem = m_ui->twElement->item(row, 4);

		double dMinValue = pMinItem->data(Qt::UserRole).toDouble();
		double dMaxValue = pMaxtem->data(Qt::UserRole).toDouble();
		if (dMinValue < dValue && dValue < dMaxValue)
		{
			pResultItem->setText(QStringLiteral("正常"));
		}
		else
		{
			pResultItem->setText(QStringLiteral("异常"));
		}


	}
}

